﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Museum.DBAccess;

public partial class ManageCity : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindGridData();
    }

    /// <summary>
    /// Delete records from database
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
        int cityId = Convert.ToInt32(GridViewCities.DataKeys[e.RowIndex].Value);
        int result = 0;
        try
        {
            result = DBAccess.DeleteCityData(cityId);
            Alert.ShowMessage("Record Deleted successfully.");
            // Refresh the data
            BindGridData();
        }
        catch (Exception ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
        }
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindGridData();
    }

    protected void BindGridData()
    {
        DataTable dtCountries = null;
        DataTable dtCities = null;
        DataRow[] dtRows = null;
        DataTable dtNewCities = null;
        try
        {
            if (!IsPostBack)
            {
                dtCountries = DBAccess.GetAllCountries();
                ddlCountry.DataSource = dtCountries;
                ddlCountry.DataBind();
                ddlCountry.Items.FindByText("United States").Selected = true;
            }

            dtCities = DBAccess.GetAllCities();

            dtRows = dtCities.Select("CountryId=" + ddlCountry.SelectedItem.Value);

            dtNewCities = dtCities.Clone();
            foreach (DataRow dr in dtRows)
            {
                dtNewCities.ImportRow(dr);
            }
            if (dtRows.Length > 0)
            {
                GridViewCities.DataSource = dtNewCities;
                GridViewCities.DataBind();
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private string ConvertSortDirectionToSql(SortDirection sortDirection)
    {
        string newSortDirection = String.Empty;

        switch (sortDirection)
        {
            case SortDirection.Ascending:
                newSortDirection = "ASC";
                break;

            case SortDirection.Descending:
                newSortDirection = "DESC";
                break;
        }

        return newSortDirection;
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridViewCities.PageIndex = e.NewPageIndex;
        if (ViewState["gvData"] != null)
            GridViewCities.DataSource = (DataTable)ViewState["gvData"];
        GridViewCities.DataBind();
    }

    protected void gridView_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataTable dataTable = GridViewCities.DataSource as DataTable;

        if (dataTable != null)
        {
            DataView dataView = new DataView(dataTable);
            dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

            GridViewCities.DataSource = dataView;
            GridViewCities.DataBind();
        }
    }
}
